package hot100;

import java.util.HashMap;
import java.util.Map;

public class Solution523 {
    public boolean checkSubarraySum(int[] nums, int k) {
        if (nums == null || nums.length == 0) {
            return false;
        }
        if (k == 0) {
            return true;
        }
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, -1);
        int left = 0;
        for (int i = 0; i < nums.length; i++) {
            left = (left + nums[i]) % k;
            if (map.containsKey(left)) {
                int index = map.get(left);
                if (i - index > 1) {
                    return true;
                }
            } else {
                map.put(left, i);
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Solution523 solution523 = new Solution523();
        System.out.println(solution523.checkSubarraySum(new int[]{1, 0}, 2));

    }
}
